<?php

	class Request
	{               
	
		function __construct( $url = false )
		{
			$this->parseURL( $url );
			
			$this->requested_response = $_SERVER["HTTP_X_REQUESTED_RESPONSE"] ? $_SERVER["HTTP_X_REQUESTED_RESPONSE"] : "html";
		}
		
		public function parseURL( $url )
		{
			$request = preg_replace( "|/+|", "/", $url );
			$request = preg_replace( "|^/?([^\?]*)\??.*$|", "$1", $request );
			
			$url_parts = split( "/", $request, 3 );
			
			if ( isset( $url_parts[2] ) )
				list( $controller, $method, $raw_params ) = $url_parts;
			
			else if ( isset( $url_parts[1] ) )
			{
				$controller = $url_parts[0];
				$method     = $url_parts[1];
				$raw_params = $url_parts[1];
			}
			
			else
			{
				$controller = $url_parts[0];
				$method     = false;
				$raw_params = "";
			}
			
			parse_str( $raw_params, $params );
			
			$this->params             = $this->merge( $_GET, $params, $_POST, Array( "__raw" => $raw_params ) );
			$this->controller         = $controller;
			$this->method             = $method;
			
			return true;
		}
		
		public function merge()
		{
			$args = func_get_args();
			
			$output = Array();
			
			foreach( $args as $array )
				foreach( $array as $key => $value )
					$output[ strtolower( $key ) ] = $value;
				
			return $output;
		}
	}

?>
